<template>
	<view class="uni-calendar-item__weeks-box" :class="{
		'uni-calendar-item--disable':weeks.disable,
		'uni-calendar-item--isDay':isSameWeek && weeks.isDay,
		'uni-calendar-item--checked':(isSameWeek && !weeks.isDay) ,
		'uni-calendar-item--before-checked':weeks.beforeMultiple,
		'uni-calendar-item--multiple': weeks.multiple,
		'uni-calendar-item--after-checked':weeks.afterMultiple,
		}"
	 @click="choiceDate(weeks)">
		<view class="uni-calendar-item__weeks-box-item">
			<text v-if="selected&&weeks.extraInfo" class="uni-calendar-item__weeks-box-circle"></text>
			<text class="uni-calendar-item__weeks-box-text" :class="{
				'uni-calendar-item--isDay-text': weeks.isDay,
				'uni-calendar-item--isDay':isSameWeek && weeks.isDay,
				'uni-calendar-item--checked':isSameWeek && !weeks.isDay,
				'uni-calendar-item--before-checked':weeks.beforeMultiple,
				'uni-calendar-item--multiple': weeks.multiple,
				'uni-calendar-item--after-checked':weeks.afterMultiple,
				'uni-calendar-item--disable':weeks.disable,
				}">{{weeks.date}}</text>
			<text v-if="!lunar&&!weeks.extraInfo && weeks.isDay" class="uni-calendar-item__weeks-lunar-text" :class="{
				'uni-calendar-item--isDay-text':weeks.isDay,
				'uni-calendar-item--isDay':isSameWeek && weeks.isDay,
				'uni-calendar-item--checked':isSameWeek && !weeks.isDay,
				'uni-calendar-item--before-checked':weeks.beforeMultiple,
				'uni-calendar-item--multiple': weeks.multiple,
				'uni-calendar-item--after-checked':weeks.afterMultiple,
				}">{{todayText}}</text>
			<text v-if="lunar&&!weeks.extraInfo" class="uni-calendar-item__weeks-lunar-text" :class="{
				'uni-calendar-item--isDay-text':weeks.isDay,
				'uni-calendar-item--isDay':isSameWeek && weeks.isDay,
				'uni-calendar-item--checked':isSameWeek && !weeks.isDay,
				'uni-calendar-item--before-checked':weeks.beforeMultiple,
				'uni-calendar-item--multiple': weeks.multiple,
				'uni-calendar-item--after-checked':weeks.afterMultiple,
				'uni-calendar-item--disable':weeks.disable,
				}">{{weeks.isDay ? todayText : (weeks.lunar.IDayCn === '初一'?weeks.lunar.IMonthCn:weeks.lunar.IDayCn)}}</text>
			<text v-if="weeks.extraInfo&&weeks.extraInfo.info" class="uni-calendar-item__weeks-lunar-text" :class="{
				'uni-calendar-item--extra':weeks.extraInfo.info,
				'uni-calendar-item--isDay-text':weeks.isDay,
				'uni-calendar-item--isDay':isSameWeek && weeks.isDay,
				'uni-calendar-item--checked':isSameWeek && !weeks.isDay,
				'uni-calendar-item--before-checked':weeks.beforeMultiple,
				'uni-calendar-item--multiple': weeks.multiple,
				'uni-calendar-item--after-checked':weeks.afterMultiple,
				'uni-calendar-item--disable':weeks.disable,
				}">{{weeks.extraInfo.info}}</text>
		</view>
	</view>
</template>

<script>
	import {
	initVueI18n
	} from '@dcloudio/uni-i18n'
	import messages from './i18n/index.js'
	const {	t	} = initVueI18n(messages)
	export default {
		emits:['change'],
		props: {
			weeks: {
				type: Object,
				default () {
					return {}
				}
			},
			calendar: {
				type: Object,
				default: () => {
					return { }
				}
			},
			selected: {
				type: Array,
				default: () => {
					return []
				}
			},
			lunar: {
				type: Boolean,
				default: false
			},
			startFromMonday:{
				type:Boolean,
				default: false
			}
		},
			
		data(){
			return {
				isSameWeek:false
			}
		},
		watch:{
			calendar(cur){
				this.checkSame()
			}
		},
		mounted() {
			this.checkSame()
		},
		computed: {
			todayText() {
				return t("uni-calender.today")
			},
		},
        methods: {
			checkSame(){
				if(this.calendar.hasOwnProperty('fullDate')){
				    if(this.weeks.hasOwnProperty('fullDate')){
				        this.isSameWeek = this.getWeekInYear(this.weeks.fullDate) == this.getWeekInYear(this.calendar.fullDate)
				    }else if(this.weeks.disable){
				        let fullDate = `${this.weeks.lunar.cYear}-${this.weeks.month}-${this.weeks.date}`
				        this.isSameWeek = this.getWeekInYear(fullDate) == this.getWeekInYear(this.calendar.fullDate)
				    }
				}
			},
			choiceDate(weeks) {
				this.$emit('change', weeks)
			},
			getWeekInYear(str){
			 	let tempDate = new Date()
			 	const ONE_DAY = 24 * 60 * 60 * 1000
			 	let date = new Date(str)
			     //判断该星期是否跨年，如果跨年就是第一周
			 	
			 	let day = date.getDay()
			 	if(this.startFromMonday){
			 		day = day==0?6:(day-1)
			 	}
			 	tempDate.setTime(date.getTime() - day * ONE_DAY)
			     let weekStartDate = tempDate //一周开始时间
			 	tempDate.setTime(weekStartDate.getTime() + 6 * ONE_DAY)
			     let endDate = tempDate //一周结束时间
			     if (weekStartDate.getFullYear() != endDate.getFullYear()) {
			         return 1
			     }
			     let d1 = new Date(date)
			     let d2 = new Date(date)
			     d2.setMonth(0)
			     d2.setDate(1)
			     d2 = tempDate.setTime(d2.getTime() - d2.getDay() * ONE_DAY)
			     let rq = d1 - d2
			     let days = Math.ceil(rq / ONE_DAY) + 1
			     let num = Math.ceil(days / 7)
				 if(this.startFromMonday&&day==6){
				 	num--
				 }
			     return num
			}
		}
	}
</script>

<style lang="scss" scoped>
	$uni-font-size-base:14px;
	$uni-text-color:#333;
	$uni-font-size-sm:12px;
	$uni-color-error: #e43d33;
	$uni-opacity-disabled: 0.3;
	$uni-text-color-disable:#c0c0c0;
	$uni-primary: #2979ff !default;
	.uni-calendar-item__weeks-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.uni-calendar-item__weeks-box-text {
		font-size: $uni-font-size-base;
		color: $uni-text-color;
	}

	.uni-calendar-item__weeks-lunar-text {
		font-size: $uni-font-size-sm;
		color: $uni-text-color;
	}

	.uni-calendar-item__weeks-box-item {
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		width: 100rpx;
		height: 100rpx;
	}

	.uni-calendar-item__weeks-box-circle {
		position: absolute;
		top: 5px;
		right: 5px;
		width: 8px;
		height: 8px;
		border-radius: 8px;
		background-color: $uni-color-error;

	}

	.uni-calendar-item--disable {
		background-color: rgba(249, 249, 249, $uni-opacity-disabled);
		color: $uni-text-color-disable;
	}

	.uni-calendar-item--isDay-text {
		color: $uni-primary;
	}

	.uni-calendar-item--isDay {
		background-color: $uni-primary;
		opacity: 0.8;
		color: #fff;
	}

	.uni-calendar-item--extra {
		color: $uni-color-error;
		opacity: 0.8;
	}

	.uni-calendar-item--checked {
		background-color: $uni-primary !important;
		color: #fff;
		opacity: 0.8;
	}

	.uni-calendar-item--multiple {
		background-color: $uni-primary;
		color: #fff;
		opacity: 0.8;
	}
	.uni-calendar-item--before-checked {
		background-color: #ff5a5f;
		color: #fff;
	}
	.uni-calendar-item--after-checked {
		background-color: #ff5a5f;
		color: #fff;
	}
</style>
